package com.dangz.dans.controller;

import com.alibaba.fastjson.JSONObject;
import com.dangz.dans.dao.ClientInfoRepository;
import com.dangz.dans.dao.ComUserRepository;
import com.dangz.dans.dao.MessageRepository;
import com.dangz.dans.entity.CommonResponse;
import com.dangz.dans.model.ClientInfo;
import com.dangz.dans.model.Message;
import com.dangz.dans.model.User;
import com.dangz.dans.response.ApiEnum;
import com.dangz.dans.response.CodeEnum;
import com.dangz.dans.response.TypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

/**
 * @author dangz
 * @since 2018/7/6 10:05
 */
@RestController
@RequestMapping("dans/user")
@Api(tags = "用户操作")
public class ComUserController {
    @Autowired
    private ComUserRepository userRepository;
    @Autowired
    private ClientInfoRepository clientInfoRepository;
    @Autowired
    private MessageRepository messageRepository;
    @ApiOperation("登陆")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", value = "登录名", name = "userName", dataType = "String", required = true),
            @ApiImplicitParam(paramType = "query", value = "密码", name = "pwd", dataType = "String", required = true)
    })
    @PostMapping(value = "login")
    public CommonResponse login(String userName, String pwd) {
        CommonResponse response = null;
        JSONObject obj = null;
        try {
            User user = userRepository.findByUserIdAndUserPwd(userName, pwd);
            if (user == null) {
                response = new CommonResponse(TypeEnum.getFullRespCode(TypeEnum.Success,
                        ApiEnum.XTGL, CodeEnum.SUCCESS_NO_DATA_LOCAL), CodeEnum.SUCCESS_NO_DATA_LOCAL.getInfo());
            } else {
                //根据用户名生成ClientInfo
//                ClientInfo clientInfo = new ClientInfo();
//                clientInfo.setClientId(UUID.randomUUID().toString().replace("-", ""));
//                clientInfo.setUserId(userName);
//                clientInfoRepository.save(clientInfo);
//                user.setClientId(clientInfo.getClientId());
                obj = new JSONObject();
                obj.put("userInfo", user);

                List<Message> messages = messageRepository.findAllByToAndStatusOrderByCreateTimeDesc(userName, 0);
                messageRepository.updateStatusByTo(1, userName);
                obj.put("messages", messages);
                response = new CommonResponse(TypeEnum.getFullRespCode(TypeEnum.Success,
                        ApiEnum.XTGL, CodeEnum.SUCCESS_LOCAL), CodeEnum.SUCCESS_LOCAL.getInfo(), obj);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }

    @ApiOperation("查询所有用户")
    @PostMapping(value = "findAll")
    public CommonResponse findAll() {
        CommonResponse response = null;
        try {
            List<User> users = userRepository.findAll();
            if (users.size() == 0) {
                response = new CommonResponse(TypeEnum.getFullRespCode(TypeEnum.Success,
                        ApiEnum.XTGL, CodeEnum.SUCCESS_NO_DATA_LOCAL), CodeEnum.SUCCESS_NO_DATA_LOCAL.getInfo());
            } else {
                response = new CommonResponse(TypeEnum.getFullRespCode(TypeEnum.Success,
                        ApiEnum.XTGL, CodeEnum.SUCCESS_LOCAL), CodeEnum.SUCCESS_LOCAL.getInfo(), users);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }
}
